package com.yzx.lifeassistants.receiver;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import cn.bmob.push.PushConstants;

import com.yzx.lifeassistants.R;
import com.yzx.lifeassistants.activity.NewOfficeInfoActivity;
import com.yzx.lifeassistants.bean.OfficeInform;
import com.yzx.lifeassistants.common.CommonConstant;
import com.yzx.lifeassistants.utils.LogcatUtils;
import com.yzx.lifeassistants.utils.SpUtils;

/**
 * @Description: 接受推送消息
 * @author: yzx
 * @time: 2016-1-4 上午10:54:29
 */
public class MyPushMessageReceiver extends BroadcastReceiver {
	protected SpUtils sputil;// 保存
	private Boolean isUpdate = false;// 是否是更新
	private Boolean isInfo = false;// 是否是教务处通知
	private String downloadUrl;// 最新版下载地址
	private String ticker;// 状态栏通知文字
	private String title = "找我";// 下拉通知标题
	private String content;// 下拉通知内容
	private OfficeInform info;// 教务处通知

	@SuppressWarnings("deprecation")
	@Override
	public void onReceive(Context context, Intent intent) {
		if (intent.getAction().equals(PushConstants.ACTION_MESSAGE)) {
			String content = intent
					.getStringExtra(PushConstants.EXTRA_PUSH_MESSAGE_STRING);
			LogcatUtils.d("bmob", "客户端收到推送内容：" + content);
			if (null == sputil) {
				sputil = new SpUtils(context, CommonConstant.SP_FAIL_NAME);
			}
			// 是否开启推送通知
			Boolean isReceive = sputil.getValue(CommonConstant.INFO_KEY, false);
			if (isReceive) {
				try {
					JSONObject object = new JSONObject(content);
					String type = object.getString("type");
					if ("update".equals(type)) {
						isUpdate = true;
						downloadUrl = object.getString("url");
						ticker = "找我App更新啦~";
						this.content = "赶紧点我来更新吧~";
					} else if ("officeInfo".equals(type)) {
						isInfo = true;
						JSONObject infoObject = object
								.getJSONObject("officeInfo");
						info = new OfficeInform();
						info.setTitle(infoObject.getString("title"));
						info.setDate(infoObject.getString("date"));
						info.setDetail(infoObject.getString("content"));
						ticker = "您有教务处的通知~";
						this.content = "赶紧点我来查看通知内容~";
					}
				} catch (JSONException e1) {
					e1.printStackTrace();
				}

				// 消息通知栏
				// 定义NotificationManager
				String ns = Context.NOTIFICATION_SERVICE;
				NotificationManager mNotificationManager = (NotificationManager) context
						.getSystemService(ns);

				// 定义通知栏展现的内容信息
				int icon = R.drawable.ic_launcher;// 通知图标
				CharSequence tickerText = ticker;// 状态栏(Status Bar)显示的通知文本提示
				long when = System.currentTimeMillis();// 通知产生的时间，会在通知信息里显示
				Notification notification = new Notification(icon, tickerText,
						when);
				notification.flags = Notification.FLAG_AUTO_CANCEL;// 实现点击后消失
				long[] vibrate = { 0, 100, 200, 300 };
				notification.vibrate = vibrate;// 手机振动

				// 定义下拉通知栏时要展现的内容信息
				CharSequence contentTitle = title;
				CharSequence contentText = this.content;
				if (isUpdate) {
					if (null == downloadUrl || "".equals(downloadUrl)) {
						downloadUrl = CommonConstant.DOWNLOAD_URL;
					}
					Intent notificationIntent = new Intent(Intent.ACTION_VIEW,
							Uri.parse(downloadUrl));
					PendingIntent contentIntent = PendingIntent.getActivity(
							context, 0, notificationIntent, 0);
					notification.setLatestEventInfo(context, contentTitle,
							contentText, contentIntent);
					// 用mNotificationManager的notify方法通知用户生成标题栏消息通知
					mNotificationManager.notify(1, notification);
				} else if (isInfo) {
					Intent notificationIntent = new Intent(context,
							NewOfficeInfoActivity.class);
					Bundle bundle = new Bundle();
					bundle.putSerializable("newOfficeInfo", info);
					notificationIntent.putExtras(bundle);
					PendingIntent contentIntent = PendingIntent.getActivity(
							context, 0, notificationIntent,
							PendingIntent.FLAG_UPDATE_CURRENT);
					notification.setLatestEventInfo(context, contentTitle,
							contentText, contentIntent);
					// 用mNotificationManager的notify方法通知用户生成标题栏消息通知
					mNotificationManager.notify(1, notification);
				}
			}

		}
	}
}
